var util = require('../../../utils/util.js');
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    code:'',
    info: {},  //个人基本信息
    isConfirm:false,  //解绑或注销的弹窗
    time: 30,    //倒计时
    numshow: true,  // ‘发送’字样
    setInter: '',  //定时器
    content:'',
    KeyboardKeys: [1, 2, 3, 4, 5, 6, 7, 8, 9, '·', 0], //键盘
    issuper:false,  //员工列表弹窗
    ismove_per:false,  //确定移交人弹窗
    iscopy:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData.loginInfo);
    this.setData({
      is_auto:options.is_auto,
      porgram_employee_id:app.globalData.loginInfo.porgram_employee_id,  //是否被后台绑定的小程序
      nickname:app.globalData.loginInfo.nickname,
      raplace_h:0.8*wx.getSystemInfoSync().windowHeight,
      set_receved:app.globalData.loginInfo.set_receved,
      type_level:app.globalData.type_level,
      is_manage: app.globalData.loginInfo.is_manage,
      cur_manage_id:app.globalData.person_id,  //当前管理员id
      isaudio:wx.getStorageSync('audio')
    });
  },
  // 页面数据
  update:function(){
    var that = this;
    app.AppHttp('api/Client/personalSet', {
      openid: app.globalData.openid
    }, function (res) {
      console.log(res.data);
      that.setData({
        info: res.data
      }); 
      if(that.data.is_auto==1){
        that.jiebang();
      };    
    });
    // 权限
    wx.request({
      url: app.globalData.url + 'api/Client/KitchenManage',
      method: "POST",
      data: {
        id: app.globalData.loginInfo.id
      },
      success: function (res) {
        console.log(res.data.ClientFind.check_open_close);
        app.globalData.check_open_close = res.data.ClientFind.check_open_close;
        that.setData({
          chuFang: res.data,
          check_open_close:res.data.ClientFind.check_open_close,
          order_open_close: res.data.ClientFind.order_open_close
        });
      }
    });
  },
  // 审核权限开关
  sheheswi: function () {
    var that=this;
    console.log(app.globalData.loginInfo.id+'--'+that.data.check_open_close)
    
    wx.request({
      url: app.globalData.url + 'api/Client/setCheckOpenClose',
      method: "POST",
      data: {
        id: app.globalData.loginInfo.id,
        status: that.data.check_open_close
      },
      success: function (res) {
        console.log(JSON.stringify(res.data));
        wx.showToast({
          title: res.data.msg,
        }, 2000)
      }
    });
    var check_open_close = that.data.check_open_close;
    if (check_open_close==0){
      check_open_close=1;
    } else if (check_open_close==1){
      check_open_close =0;
    }
    that.setData({
      check_open_close: check_open_close
    });
  },
  // 解绑或注销弹窗
  jiebang:function(){
    this.setData({
      is_auto:0,
      isConfirm: true,
      tel: this.data.info.phone.substr(0, 3) + '****' + this.data.info.phone.substr(7)
    })
  },
  // 关闭解绑或注销弹窗
  confirmNOkClick:function(){
    util.hideBuyModal(this,'isphone_test');
    this.setData({
      isConfirm: false,
      iscopy:0
    })
  },
  // 继续解绑或注销
  confirmOkClick:function(){
    util.showBuyModal(this);
    this.setData({
      isphone_test:true,
      valid_type:'zhu_or_unbind'
    })
  },
  // 解绑与企业的关系
  confirmClickQiYe(){
    let that = this;
    app.AppHttp('api/Supplier/removeBind', {
      client_id:app.globalData.loginInfo.id,
      person_id:app.globalData.person_id
    }, function (res) {
      wx.showToast({
        title: res.data.msg,
        duration: 500
      })
      if (res.data.status == 1) {
        wx.clearStorage(); //清除本地数据
        app.globalData.list=[];
        wx.reLaunch({
          url: "/pages/shangpin/shangpin"
        })
      } 
    })
  },
  // 获取验证码
  getcode: function (event) {
    var that = this;
    var times = that.times
    wx.request({
      url: app.globalData.url + 'api/Client/AjaxVerificationCode',
      method: "POST",
      data: {
        phone: that.data.info.phone
      },
      success: function (res) {
        wx.showToast({
          title: res.data.msg,
          duration: 500
        }, 2000);
      }
    });
    that.setData({
      numshow: false
    });
    that.data.setInter = setInterval(times, 1000);
  },
  // 倒计时
  times: function () {
    var time = this.data.time;
    time--;
    if (time == 0) {
      this.setData({
        numshow: true,
        time: 10
      });
      clearInterval(this.data.setInter);
      return;
    }
    this.setData({
      time: time
    });
  },
  // 键盘按键
  keyTap(e) {
    util.keyfunction(e,this,3);
    this.setData({
      iscopy:0
    });
    this.setValue();
  },
  setValue: function () {
    this.setData({
      code: this.data.content
    });
  },
  // 显示粘贴
  showcopy:function(){
    var iscopy=this.data.iscopy;
    if(iscopy<2){
      iscopy++
    }else{
      iscopy=0;
    }
    this.setData({
      iscopy:iscopy
    });
  },
  // 隐藏粘贴
  hidecopy:function(){
    this.setData({
      iscopy:0
    });
  },
  // 粘贴验证码
  tieText:function(){
    var that = this;
    wx.getClipboardData({
      success: function(res){
        if(res.data && (/^\d{4}$/).test(res.data)){
          that.setData({
            content:res.data,
            code:res.data,
            iscopy:0
          });
        };          
      }
    })
  },
  // 确定解绑或注销
  confirm_finally:function(){
    var that = this;
    if (that.data.is_manage == 1) {
      app.AppHttp('api/Client/UnbindClient', {
        phone: that.data.info.phone,
        code:that.data.code,
        openid: app.globalData.openid
      }, function (res) {
        console.log(res.data);
        wx.showToast({
          title: res.data.msg,
          duration: 500
        })
        if (res.data.status == 1) {
          wx.clearStorage(); //清除本地数据
          app.globalData.list=[];
          wx.reLaunch({
            url: "/pages/shangpin/shangpin"
          })
        } 
      })
    } else if (that.data.is_manage == 2) {
      app.AppHttp('api/Client/CancellationAccount', {
        phone: that.data.info.phone,
        code: that.data.code,
        openid: app.globalData.openid
      }, function (res) {
        wx.showToast({
          title: res.data.msg,
          duration: 500
        })
        if (res.data.status==1){
          wx.clearStorage(); //清除本地数据
          app.globalData.list=[];
          wx.reLaunch({
            url: "/pages/shangpin/shangpin"
          })
        }    
      })
    }
    that.setData({
      isConfirm: false
    })
  },
  // 上传个人头像
  changehead:function(){
    wx.navigateTo({
      url: '../../cropper/cropper?imgx=2'
    })
  },
  // 修改真实姓名
  changerealname:function(){
    wx.navigateTo({
      url: '../genggainame/genggainame'
    })
  },
  // 修改手机号码
  changephone:function(){
    var info =this.data.info;
    wx.navigateTo({
      url: '../genggaiphone/genggaiphone?phone='+info.phone
    })
  },
  // 设置收货
  setshouhuo:function(){
    var that = this;
    var set_receved=that.data.set_receved==1?0:1
    app.AppHttp('api/Client/setReceved', {
      id: app.globalData.loginInfo.id,
      set_receved:set_receved
    }, function (res) {
      console.log(res.data);
      app.globalData.loginInfo.set_receved=set_receved;
      that.setData({
        set_receved:set_receved
      });
    });
  },
  // 门店员工列表
  open_employee:function(){
    var that = this;
    util.showBuyModal(this);
    that.setData({
      issuper:true
    })
    app.AppHttp('api/Client/getAllPersonal', {
      id: app.globalData.loginInfo.id,
    }, function (res) {
      console.log(JSON.stringify(res.data));
        that.setData({
          datas: res.data
        });
    });
  },
  // 关闭员工列表
  close_super:function(){
    util.hideBuyModal(this,'issuper');
  },
  // 选定移交人
  super_conf:function(e){
    var index=e.currentTarget.dataset.index;
    util.hideBuyModal(this,'issuper');
    this.setData({
      chose_employ_id:this.data.datas[index].id,
      chose_employ:this.data.datas[index].real_name && this.data.datas[index].real_name!=0?this.data.datas[index].real_name:this.data.datas[index].nickname,
      ismove_per:true
    })
  },
  // 关闭确定移交人
  close_move_per:function(){
    this.setData({
      ismove_per:false
    });
  },
  // 打开移交验证
  conf_move_per:function(){
    util.showBuyModal(this);
    this.setData({
      isphone_test:true,
      valid_type:'move',
      ismove_per:false
    });
  },
  // 确定移交
  conf_fin_move:function(){
    var that = this;
    if(that.data.code==''){
      wx.showLoading({
        title:'请输入验证码',
        duration:500
      });
      return;
    };
    app.AppHttp('api/Client/transferManage', {
      openid: app.globalData.openid,
      id: that.data.chose_employ_id,
      code:that.data.code,
      phone: that.data.info.phone
    }, function (res) {
      console.log(JSON.stringify(res.data));
      util.hideBuyModal(that,'isphone_test');
      if(res.data.status==1){
        that.setData({
          is_manage:1
        });
      };
      wx.showToast({
        title: res.data.msg,
      }, 2000);
    });
  },
  audio_change:function(){
    var isaudio=!this.data.isaudio;
    this.setData({
      isaudio:isaudio
    });
    wx.setStorageSync('audio',isaudio);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (app.globalData.imgSrc2 != '') {
      that.setData({
        imgSrc: app.globalData.imgSrc2
      });
      wx.uploadFile({
        url: app.globalData.url + 'api/Client/setHeadPortrait',
        filePath: that.data.imgSrc,
        name: 'set_head_portrait',
        method: "POST",
        formData: {
          openid: app.globalData.openid
        },
        success: function (res) {
          console.log(JSON.stringify(res.data));
          that.update();
        },
        fail: function (res) {
          wx.showModal({
            title: '错误提示',
            content: '上传图片失败',
            showCancel: false
          });
          that.update();
        }
      });
    }else{
      that.update();
    };
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    //清除计时器  即清除setInter
    clearInterval(that.data.setInter)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.update();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})